package biopepa;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

//import parserBiopepa.*;

public class Automaton {
		private String autoName;
		private  HashMap<String , String > autoVariables; //variabile + espressione valore iniziale
		public  HashMap<String,NewParameter> autoParameters;
		public  HashMap<String,NewParameter> autoExpressions;
		public HashMap<String , DiscreteTrans> autoDiscreteTrans;
		private boolean isContinuous;
	
		
		public Automaton(NewSpecies species , LocationAndInitial locationAndInitial , BiopepaModel model){
			
			if(model.hasOneLocation == true){
				autoName = species.speciesName ;
				autoVariables = new HashMap<String,String>();
				autoParameters = new HashMap<String,NewParameter>();
				autoExpressions = new HashMap<String,NewParameter>();
				autoDiscreteTrans = new HashMap<String , DiscreteTrans>();			
				isContinuous = species.isContinuous;
				
					if(isContinuous == true){
						autoVariables.put(species.speciesName , String.valueOf(species.speciesInitial) + "*" + locationAndInitial.location.stepSizeVariableC);
					}
					else{
						autoVariables.put(species.speciesName , String.valueOf(species.speciesInitial) + "*" + locationAndInitial.location.stepSizeVariableD);
					}
					
				Iterator iterator1 = species.getActionsEnabledObjects().keySet().iterator();  
				while (iterator1.hasNext()) {  
				   String key = iterator1.next().toString();  
				   DiscreteTrans newDiscreteTrans;		
				   newDiscreteTrans = new DiscreteTrans(this , locationAndInitial.location , species , species.getActionsEnabledObjects().get(key) , model);		   
				   autoDiscreteTrans.put(newDiscreteTrans.discreteName , newDiscreteTrans);
				}
			}
			else{
					
					autoName = species.speciesName + "." + locationAndInitial.location.locationName;
					autoVariables = new HashMap<String,String>();
					autoParameters = new HashMap<String,NewParameter>();
					autoExpressions = new HashMap<String,NewParameter>();
					autoDiscreteTrans = new HashMap<String , DiscreteTrans>();			
					isContinuous = species.isContinuous;
					if(isContinuous == true){
						autoVariables.put(species.speciesName + "." + locationAndInitial.location.locationName , String.valueOf(locationAndInitial.initial) + "*" + locationAndInitial.location.stepSizeVariableC);
					}
					else{
						autoVariables.put(species.speciesName + "." + locationAndInitial.location.locationName, String.valueOf(locationAndInitial.initial) + "*" + locationAndInitial.location.stepSizeVariableD);
					}
					Iterator iterator1 = species.getActionsEnabledObjects().keySet().iterator();  
					while (iterator1.hasNext()) {  
					   String key = iterator1.next().toString();  
					   Iterator iterator1b = species.getActionsEnabledObjects().get(key).locations.keySet().iterator();  
						while (iterator1b.hasNext()) {  
						   String key2 = iterator1b.next().toString(); 
						   
						   if(locationAndInitial.location == species.getActionsEnabledObjects().get(key).locations.get(key2)){
							   DiscreteTrans newDiscreteTrans;		
							   ActionEnabled action = species.getActionsEnabledObjects().get(key);
							   
							   if(action.isDisplacement == false){
								   Iterator iterator1c = action.locations.keySet().iterator();
								   while(iterator1c.hasNext()){
									   String key3 = iterator1c.next().toString();
									   NewLocation locActionEnabled = action.locations.get(key3);
									   if(locActionEnabled.locationName.equals(locationAndInitial.location.locationName)){
										   newDiscreteTrans = new DiscreteTrans(this , locationAndInitial.location , species , action , model);
										   autoDiscreteTrans.put(newDiscreteTrans.discreteNameWith , newDiscreteTrans);
									   }
								   }
							   }
							   else{
								   String locName = locationAndInitial.location.locationName;			   
								   NewLocation loc1 = action.location1;
								   NewLocation loc2 = action.location2;
								   if(locName.equals(loc1.locationName) == true){
									   newDiscreteTrans = new DiscreteTrans(this, true , locationAndInitial.location , species , action , model);
									   newDiscreteTrans.isDisplacement = true;
									   autoDiscreteTrans.put(newDiscreteTrans.discreteName , newDiscreteTrans );
								   }
								   if(locName.equals(loc2.locationName) == true){
									   newDiscreteTrans = new DiscreteTrans(this, false , locationAndInitial.location , species , action , model);
									   newDiscreteTrans.isDisplacement = true;
									   autoDiscreteTrans.put(newDiscreteTrans.discreteName , newDiscreteTrans );
								   }
							   }
						   }
						}
						
						  
					}
				
				
			}
			
		    
		}
		
		
		public Automaton(String name){
			autoName = name;
			autoVariables = new HashMap<String,String>();
			autoParameters = new HashMap<String,NewParameter>();
			autoExpressions = new HashMap<String,NewParameter>();
			autoDiscreteTrans = new HashMap<String , DiscreteTrans>();
			isContinuous = false;
		}
		
		public  String getAutoName(){
			return autoName;
			
		}
		
		public HashMap<String , String> getVariables(){
			return this.autoVariables;
		}
		
		public HashMap<String , DiscreteTrans> getAutoDiscreteTrans(){
			return this.autoDiscreteTrans;
		}
		
		public boolean isContinuous(){
			return this.isContinuous;
		}

		
		

}

